﻿using Iworld.Backstage.Framework.AdministratorService;
using Iworld.ViewModel;
using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
namespace Iworld.Backstage.Framework
{
    /// <summary>
    /// 管理员列表视图模型
    /// </summary>
    public class AdministratorListViewModel : ShowListViewModelBase<AdministratorExport, AdministratorServiceClient>
    {
        string keyword;
        public DateTime? beginTime = null;
        public DateTime? endTime = null;

        ObservableCollection<FilterBoxModel> groups = new ObservableCollection<FilterBoxModel>();

        public ObservableCollection<FilterBoxModel> Groups
        {
            get { return groups; }
            set
            {
                if (groups == value) { return; }
                groups = value;
                OnPropertyChanged("Groups");
            }
        }

        public string Keyword
        {
            get { return keyword; }
            set
            {
                if (keyword == value) { return; }
                keyword = value == "" ? null : value;
                OnPropertyChanged("Keyword");
                Refresh(1);
            }
        }
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? BeginTime
        {
            get
            {
                return beginTime;
            }
            set
            {
                if (beginTime != value)
                {
                    beginTime = value;
                    OnPropertyChanged("BeginTime");
                    Refresh(1);
                }
            }
        }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime
        {
            get
            {
                return endTime;
            }
            set
            {
                if (endTime != value)
                {
                    endTime = value;
                    OnPropertyChanged("EndTime");
                    Refresh(1);
                }
            }
        }

        public AdministratorListViewModel()
            : base(PageCategory.管理员管理.ToString(),Page.管理员列表.ToString())
        {
            Groups.Add(new FilterBoxModel("正在努力加载用户组...", null, null));
            this.client.GetAdministratorsCompleted += 
                this.WrapEventHandler<GetAdministratorsCompletedEventArgs>(GetAdministratorsCompleted);
            this.client.GetAdministratorGroupsCompleted += this.WrapEventHandler<GetAdministratorGroupsCompletedEventArgs>(client_GetAdministratorGroupsCompleted);
            this.client.GetAdministratorGroupsAsync(new GetAdministratorGroupsImport() {
                PageIndex = 1,
                Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token
            });
        }

        void client_GetAdministratorGroupsCompleted(object sender, GetAdministratorGroupsCompletedEventArgs e)
        {
            if(e.Result.Success)
            {
                Groups.Clear();
                Groups.Add(new FilterBoxModel("全部", null, Refresh, true));
                foreach(var k in e.Result.List)
                {
                  Groups.Add(new FilterBoxModel(k.Name,k.Id, Refresh));
                }
            }
            else
            {
                this.ShowError(e.Result.Error);
            }
        }

        void GetAdministratorsCompleted(object sender, GetAdministratorsCompletedEventArgs e)
        {
            if (e.Result.Success)
            {
                this.PageIndex = e.Result.PageIndex;
                this.TotalPage = e.Result.CountOfPage;
                UpdateLsit(e.Result.List);
            }
            else
            {
                ShowError(e.Result.Error);
            }
        }

        protected override void Refresh(object obj)
        {
            int pageIndex = obj == null ? this.PageIndex : Convert.ToInt32(obj);
            int? gId=null;
            FilterBoxModel fm = Groups.FirstOrDefault(x => x.Selected);
            if (fm!=null)
            {
                gId = (int?)fm.Value;
            }
            client.GetAdministratorsAsync(new GetAdministratorsImport()
            {
                PageIndex = pageIndex, 
                BeginTime=this.BeginTime,
                EndTime=this.EndTime, 
                Keyword=this.Keyword,
                GroupId=gId,
                Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token
            });
        }

        protected override void Reset(object obj)
        { 
            this.PageIndex = 1;
            this.BeginTime = null;
            this.EndTime = null;
            Keyword = null;
            Refresh(null);
        }
    }
}
